class SettingsStore {
  constructor() {
    // 初始化设置数据
    this.initializeSettings();
  }

  // 初始化设置
  initializeSettings() {
    // 从localStorage加载设置，如果不存在则使用默认值
    const savedSettings = localStorage.getItem('systemSettings');
    
    if (savedSettings) {
      this.settings = JSON.parse(savedSettings);
    } else {
      this.settings = {
        // 语言设置
        language: 'zh', // 默认中文
        availableLanguages: [
          { code: 'zh', name: '中文' },
          { code: 'en', name: 'English' },
          { code: 'fr', name: 'Français' },
          { code: 'de', name: 'Deutsch' },
          { code: 'es', name: 'Español' },
          { code: 'ja', name: '日本語' }
        ],
        
        // 管理员账号
        administrators: [
          { id: 1, username: 'admin', password: 'admin123', role: 'superadmin', createdAt: new Date().toISOString() }
        ],
        
        // 仪表盘设置
        dashboard: {
          components: [
            { id: 'overview', name: '概览', enabled: true, order: 1 },
            { id: 'recentOrders', name: '最近订单', enabled: true, order: 2 },
            { id: 'topProducts', name: '热销产品', enabled: true, order: 3 },
            { id: 'trafficStats', name: '流量统计', enabled: true, order: 4 },
            { id: 'newsFeed', name: '新闻动态', enabled: true, order: 5 }
          ],
          backgroundColor: '#f5f5f5',
          darkMode: false
        }
      };
      
      this.saveToLocalStorage();
    }
  }

  // 保存设置到localStorage
  saveToLocalStorage() {
    localStorage.setItem('systemSettings', JSON.stringify(this.settings));
  }

  // 获取所有设置
  getAllSettings() {
    return this.settings;
  }

  // ==================== 语言设置 ==================== //
  
  // 获取当前语言
  getCurrentLanguage() {
    return this.settings.language;
  }

  // 设置语言
  setLanguage(languageCode) {
    const language = this.settings.availableLanguages.find(lang => lang.code === languageCode);
    if (language) {
      this.settings.language = languageCode;
      this.saveToLocalStorage();
      return true;
    }
    return false;
  }

  // 获取可用语言列表
  getAvailableLanguages() {
    return this.settings.availableLanguages;
  }

  // ==================== 管理员账号管理 ==================== //
  
  // 获取所有管理员账号
  getAdministrators() {
    return this.settings.administrators;
  }

  // 添加管理员账号
  addAdministrator(username, password, role = 'admin') {
    // 检查用户名是否已存在
    if (this.settings.administrators.some(admin => admin.username === username)) {
      return { success: false, message: '用户名已存在' };
    }
    
    const newAdmin = {
      id: Date.now(),
      username,
      password,
      role,
      createdAt: new Date().toISOString()
    };
    
    this.settings.administrators.push(newAdmin);
    this.saveToLocalStorage();
    return { success: true, message: '管理员添加成功' };
  }

  // 删除管理员账号
  deleteAdministrator(id) {
    // 不能删除最后一个管理员
    if (this.settings.administrators.length <= 1) {
      return { success: false, message: '至少需要保留一个管理员账号' };
    }
    
    const index = this.settings.administrators.findIndex(admin => admin.id === id);
    if (index !== -1) {
      this.settings.administrators.splice(index, 1);
      this.saveToLocalStorage();
      return { success: true, message: '管理员删除成功' };
    }
    return { success: false, message: '管理员不存在' };
  }

  // 更新管理员密码
  updateAdministratorPassword(id, newPassword) {
    const admin = this.settings.administrators.find(admin => admin.id === id);
    if (admin) {
      admin.password = newPassword;
      this.saveToLocalStorage();
      return { success: true, message: '密码更新成功' };
    }
    return { success: false, message: '管理员不存在' };
  }

  // ==================== 仪表盘设置 ==================== //
  
  // 获取仪表盘设置
  getDashboardSettings() {
    return this.settings.dashboard;
  }

  // 更新仪表盘组件设置
  updateDashboardComponents(components) {
    this.settings.dashboard.components = components;
    this.saveToLocalStorage();
  }

  // 更新仪表盘背景颜色
  updateDashboardBackgroundColor(color) {
    this.settings.dashboard.backgroundColor = color;
    this.saveToLocalStorage();
  }

  // 切换暗黑模式
  toggleDarkMode(enabled) {
    this.settings.dashboard.darkMode = enabled;
    this.saveToLocalStorage();
  }

  // 更新组件顺序
  updateComponentOrder(componentId, newOrder) {
    const component = this.settings.dashboard.components.find(c => c.id === componentId);
    if (component) {
      component.order = newOrder;
      // 重新排序所有组件
      this.settings.dashboard.components.sort((a, b) => a.order - b.order);
      this.saveToLocalStorage();
    }
  }

  // 切换组件显示状态
  toggleComponent(componentId) {
    const component = this.settings.dashboard.components.find(c => c.id === componentId);
    if (component) {
      component.enabled = !component.enabled;
      this.saveToLocalStorage();
    }
  }
}

// 导出单例实例
export default new SettingsStore();